package com.sskj.tibi.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hey.lib.HeySpinner;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.bean.rxbus.AddressPostBean;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.tibi.R;
import com.sskj.tibi.R2;
import com.sskj.tibi.bean.WithdrawBalanceBean;
import com.sskj.tibi.bean.WithdrawRechargeCoinBean;
import com.sskj.tibi.component.DaggerUserDataComponent;
import com.sskj.tibi.presenter.WithdrawActivityPresenter;

import java.math.BigDecimal;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;


@Route(path = RConfig.TIBI_WITHDRAW)//提币
public class WithdrawActivity extends BaseActivity<WithdrawActivityPresenter> {
    @BindView(R2.id.heySpinner)
    HeySpinner heySpinner;
    @BindView(R2.id.tvAvailable)
    TextView tvAvailable;
    @BindView(R2.id.etAddress)
    EditText etAddress;
    @BindView(R2.id.ivAddress)
    ImageView ivAddress;
    @BindView(R2.id.etNum)
    EditText etNum;
    @BindView(R2.id.tvUnit)
    TextView tvUnit;
    @BindView(R2.id.tvAll)
    TextView tvAll;
    @BindView(R2.id.tvGetNum)
    TextView tvGetNum;
    @BindView(R2.id.tvFee)
    TextView tvFee;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private BottomSheetDialog bottomSheetCoin;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private BottomSheetDialog bottomSheetDialogCheck;

    @Override
    protected int getLayoutId() {
        return R.layout.tibi_activity_withdraw;
    }

    @Override
    public WithdrawActivityPresenter getPresenter() {
        return new WithdrawActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);

        RxBus.getDefault().register(this);
        setTitle(App.INSTANCE.getString(R.string.tibi_withdrawActivity1));
        setRightImg(R.mipmap.tibi_icon_record, v -> {
            ARouter.getInstance().build(RConfig.TIBI_WITHDRAW_RECORD).navigation();
        });
        ClickUtil.click(ivAddress, () -> {
            ARouter.getInstance().build(RConfig.TIBI_ADDRESS_LIST).withBoolean(Constans.IS_SELECT, true).navigation();
        });
    }

    @Override
    protected void initData() {
        mPresenter.coinList();
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
    }

    public void updateUI(WithdrawBalanceBean data) {
        tvAvailable.setText(data.getAsset().getUsable());
        tvUnit.setText(data.getWallet_code().getCode());
        etNum.setHint(String.format(App.INSTANCE.getString(R.string.tibi_withdrawActivity2), data.getWallet_code().getWithdraw_min()));
        tvFee.setHint(String.format(App.INSTANCE.getString(R.string.tibi_withdrawActivity3), data.getHandling_fee() + ""));
        ClickUtil.click(tvAll, () -> {
            etNum.setText(data.getAsset().getUsable());
        });
        ClickUtil.click(btSubmit, () -> {
            if (checkParam()) {
                bottomSheetDialogCheck = BottomSheetUtil.showCheck(this, true, userData.getConfig().getGoogle_verify() == 1, userData.getConfig().getSms_verify() == 1, str -> {
                    bottomSheetDialogCheck.dismiss();
                    mPresenter.withdraw(etAddress.getText().toString(), etNum.getText().toString(),heySpinner.getText().toString(),  str[0], str[1], str[2]);
                });
                bottomSheetDialogCheck.show();
            }
        });
        RxTextView.afterTextChangeEvents(etNum)
                .subscribe(textViewAfterTextChangeEvent -> {
                    String s = textViewAfterTextChangeEvent.editable().toString();
                    if (TextUtils.isEmpty(s)) {
                        tvGetNum.setText("0.0000");
                        return;
                    }
                    BigDecimal subtract = new BigDecimal(s).subtract(new BigDecimal(data.getHandling_fee()));
                    double max = Math.max(0, subtract.doubleValue());
                    tvGetNum.setText(max + "");
                });
    }

    private boolean checkParam() {

        if (TextUtils.isEmpty(etAddress.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.tibi_withdrawActivity4));
            return false;
        }
        if (TextUtils.isEmpty(etNum.getText())) {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.tibi_withdrawActivity5));
            return false;
        }
//        if (TextUtils.isEmpty(codeEdt.getText())) {
//            ToastUtil.showShort(App.INSTANCE.getString(R.string.tibi_withdrawActivity6));
//            return false;
//        }
//        if (TextUtils.isEmpty(passwordEdt.getText())) {
//            ToastUtil.showShort(App.INSTANCE.getString(R.string.tibi_withdrawActivity7));
//            return false;
//        }
        return true;
    }

    public void updateCoinList(List<WithdrawRechargeCoinBean> data) {
        heySpinner.setText(data.get(0).getCode());
        mPresenter.getBalance(data.get(0).getCode());
        Flowable.fromIterable(data)
                .map(withdrawRechargeCoinBean -> withdrawRechargeCoinBean.getCode())
                .toList()
                .subscribe((strings, throwable) -> {
                            ClickUtil.click(heySpinner, () -> {
                                bottomSheetCoin = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, i, view) -> {
                                    bottomSheetCoin.dismiss();
                                    heySpinner.setText(data.get(i).getCode());
                                    mPresenter.getBalance(data.get(i).getCode());
                                }, strings);
                                bottomSheetCoin.show();
                            });

                        }
                );
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void addressBack(AddressPostBean addressPostBean) {
        if (etAddress != null) {
            etAddress.setText(addressPostBean.getAddress());
        }
    }

    public void withdrawSuccess() {
        finish();
    }
}
